<template>
    <div>
        <transition name="dialog" @before-enter="handleEnter" @before-leave="handleLeave">
            <div v-if="value" class="dialog" ref="dialog" :style="{
                opacity
            }">
                <div class="title">
                    <svg>
                        <text ref="title" text-anchor="middle" x="50%" fill="white" :stroke="strokeColor" stroke-width=".08vmax">{{title}}</text>
                    </svg>
                </div>
                <div class="message" :style="{
                    marginTop: messageY + 'vmax'
                }">
                    {{message}}
                </div>
                <div class="buttons" :class="{
                    'no-primary-buttons': !usePrimary
                }">
                    <div :class="{
                        'no-primary': !usePrimary
                    }" class="button sub">
                        <Button @click="$emit('subClick')">{{subBtnText}}</Button>
                    </div>
                    <div v-if="usePrimary" class="button primary">
                        <Button is-primary @click="$emit('primaryClick')">
                            <svg>
                                <text ref="priText" text-anchor="middle" x="50%" fill="white" :stroke="strokeColor" stroke-width=".08vmax">{{primaryBtnText}}</text>
                            </svg>
                        </button>
                    </div>
                </div>
            </div>
        </transition>
    </div>

</template>

<script lang="ts">
    import Button from "@/components/Button/Button.vue";
    import anime from 'animejs';
    const titleY = 1.8; // vmax
    const priTextY = 3; // vmax
    const strokeColor = '#002222';
    import {Vue, Component, Prop} from 'vue-property-decorator';
    import * as d3 from 'd3';
    @Component({
        components: {Button}
    })
    export default class Dialog extends Vue{
        @Prop({type: String, default: "提示"})
        title?: string;
        @Prop({type: String, default: "取消"})
        subBtnText?: string;
        @Prop({type: String, default: "OK"})
        primaryBtnText?: string;
        @Prop({type: String, default: "无提示信息。"})
        message?: string;
        @Prop({type: Number, default: 5})
        messageY?: number;
        @Prop({type: Boolean, default: false})
        usePrimary?: boolean;
        @Prop({type: Boolean, default: false})
        value?: boolean;

        strokeColor = strokeColor;
        opacity= 1;
        mounted () {
            window.addEventListener('resize', this.resize);
            this.resize();
        }
        beforeDestroy () {
            window.removeEventListener('resize', this.resize);
        }
        updated () {
            this.resize();
        }
        resize () {
            let m = Math.max(window.innerHeight,window.innerWidth) * titleY / 100;
            d3.select((this.$refs.title as HTMLElement)).attr('y', m);
            m = Math.max(window.innerHeight,window.innerWidth) * priTextY / 100;
            d3.select((this.$refs.priText as HTMLElement)).attr('y', m);
        }
        handleEnter (el:HTMLElement) {
            this.opacity = 0.4;
            anime({
                targets: this,
                opacity: 1,
                duration: 260,
                easing: 'linear',
            }).play();
        }
        handleLeave (el:HTMLElement) {
            this.opacity = 1;
            anime({
                targets: this,
                opacity: .4,
                duration: 100,
                easing: 'linear',
                update: () => {
                    (document.querySelector('.dialog') as HTMLElement).style.opacity = this.opacity.toString();
                }

            }).play();
        }
    }
</script>

<style lang="scss">
    .dialog {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        width: 45vmax;
        height: 27vmax;
        background-image: url('../assets/images/empty-dialog.png');
        background-size: 100% 100%;
        user-select: none;
        .title {
            margin-top: .5vmax;
            font-size: 1.6vmax;
            text-align: center;
            svg {
                width: 100%;
                height: 2.6vmax;
                font-size: 2.0vmax;
            }
        }
        .message {
            text-align: center;
            font-size: 1.6vmax;
            margin: 0 5vmax;
        }
        .buttons {
            position: absolute;
            bottom: 0;
            width: 100%;
            display: flex;
            justify-content: space-between;
            &.no-primary-buttons {
                justify-content: center;
                margin-bottom: 1.5vmax;
            }
            .button {
                flex: 1;
                height: 5vmax;
                padding: 1vmax .5vmax 1.5vmax;
                display: flex;
                align-items: center;
                .button-container {
                    width: 100%;
                    height: 100%;
                    button {
                        width: 100%;
                        height: 100%;
                        outline: none;
                        border: none;
                        background-size: 100% 100%;
                        font-size: 1.9vmax;
                        background-repeat: no-repeat;
                        font-family: pcr, serif;
                        background-color: transparent;

                        svg {
                            width: 100%;
                            height: 100%;
                        }
                    }
                }
                &.primary {
                    padding-right: 3vmax;
                    button {
                        background-image: url("../assets/images/priButton.png");
                    }
                }

                &.sub {
                    padding-left: 3vmax;
                    &.no-primary {
                        padding: 0;
                        flex: 0 0 21vmax;
                    }

                    button {
                        background-image: url("../assets/images/subButton.png");
                    }
                }
            }
        }
    }
    .dialog-enter ,.dialog-leave-to{
        transform: translate(-50%,-50%) scale(0.7);
    }
    .dialog-enter-to ,.dialog-leave{
        transform: translate(-50%,-50%) scale(1);
    }
    .dialog-enter-active {
        transition: transform .166s cubic-bezier(0.85, 1.79, 0.57, 1.15);
    }
    .dialog-leave-active {
        transition: transform .166s cubic-bezier(0.11,-0.6, 0.74, 0.64);
    }
</style>